import java.util.ArrayList;
import java.util.List;

public class Test1 {
    public static void main(String[] args) {
        permute(new int[]{1,2,3});
    }

    static List<List<Integer>> ret = new ArrayList<>();
    static List<Integer> path = new ArrayList<>();
    static boolean[] check;
    public static List<List<Integer>> permute(int[] nums) {
        int n = nums.length;
        check = new boolean[n];
        dfs(nums);
        return ret;
    }
    public static void dfs(int[] nums) {
        int n = nums.length;
        // if(path.size() == n) {
        //     ret.add(new ArrayList<>(path));
        //     return;
        // }

        for(int i = 0; i < n; i++) {
            if(check[i] == false) {
                path.add(nums[i]);
                check[i] = true;

                dfs(nums);

                check[i] = false;
                path.remove(path.size() - 1);
                // new Integer(nums[i])
            }

        }
        if(path.size() == n) {
            ret.add(new ArrayList<>(path));
        }
    }
}
